package com.leslie.hhcommunity.controller.interceptor;

import com.leslie.hhcommunity.entity.User;
import com.leslie.hhcommunity.service.MessageService;
import com.leslie.hhcommunity.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author
 * @PackageName:com.leslie.hhcommunity.controller.interceptor
 * @ClassnName:MessageInterceptor
 * @Description:
 * @Date 2022/1/8  22:20
 */
@Component
public class MessageInterceptor implements HandlerInterceptor {
	@Autowired
	private HostHolder hostHolder;

	@Autowired
	private MessageService messageService;

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
		User user = hostHolder.getUser();
		if (user != null && modelAndView != null) {
			int letterUnreadCount = messageService.selectLetterUnreadCount(user.getId(), null);
			int noticeUnreadCount = messageService.selectNoticeUnreadCount(user.getId(), null);
			modelAndView.addObject("allUnreadCount", letterUnreadCount + noticeUnreadCount);
		}
	}
}
